﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TireService.Data;
using System.Data;

namespace TireService.Web.Reports.Data
{
    public class ReportData
    {
        private TireServiceDB db = new TireServiceDB();

        /// <summary>
        /// Return datatable with all service records for a specific servicecar and scheduledate
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Scheduled date for service</param>
        /// <param name="ShowOnlyServiceDrives">True/false value if only service drives should be shown</param>
        /// <returns>Datatable with ServiceCarScehduleDetail information</returns>
        public DataTable GetServiceCarSchedule(int ServiceCarId, DateTime ScheduleDate, bool ShowOnlyServiceDrives)
        {
            List<ServiceCarScheduleDetailView> scheduledetails = new List<ServiceCarScheduleDetailView>();

            // Get all scheduledetails
            if (ShowOnlyServiceDrives)
            {
                scheduledetails = this.db.ServiceCarScheduleDetailViews.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate & p.ScheduleDetailTypeId == (int)Shared.ScheduleDetailType.ServiceDrive).OrderBy(p => p.SequenceNumber).OrderBy(d => d.StartTime).ToList();
            }
            else
            {
                scheduledetails = this.db.ServiceCarScheduleDetailViews.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate).OrderBy(p => p.SequenceNumber).OrderBy(d => d.StartTime).ToList();
            }
            
            // Create datatable
            DataTable dtDataTable = new DataTable();

            #region Create columns in datatable

            DataColumn dcDataColumn;

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.Int32");
            dcDataColumn.ColumnName = "ScheduleDetailTypeId";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "SubscriptionNumber";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "ContainerCargoRoom";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "SequenceNumber";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Timespan";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Address";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "DrivingDistance";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "EstimatedDrivingTime";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "CustomerName";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Phone";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "CarBrandDescription";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "RegistrationNumber";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Model";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Color";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "SocketWrenchSize";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "HubCaps";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "LockingBolt";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "RunFlatTire";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "RimProtection";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "ExtraBolts";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Dimensions";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Barcode";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Note";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "CustomerNote";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "CustomerConfirmed";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "TaskId";
            dtDataTable.Columns.Add(dcDataColumn);

            #endregion

            #region Insert rows into datatable

            foreach (ServiceCarScheduleDetailView scheduledetail in scheduledetails)
            {
                DataRow row;

                row = dtDataTable.NewRow();
                row["ScheduleDetailTypeId"] = scheduledetail.ScheduleDetailTypeId;
                row["SubscriptionNumber"] = scheduledetail.CustomerId.ToString() + "-" + scheduledetail.SubscriptionId.ToString() + "-" + scheduledetail.CustomerSubscriptionId.ToString();
                row["ContainerCargoRoom"] = scheduledetail.CargoRoomNumber.ToString() + "-" + scheduledetail.ContainerNumber;
                row["SequenceNumber"] = scheduledetail.SequenceNumber.ToString();
                row["Timespan"] = scheduledetail.StartTime.ToString() + "-" + scheduledetail.EndTime.ToString();
                row["Address"] = scheduledetail.ToAddress + ", " + scheduledetail.ToZipCode + " " + scheduledetail.ToCity;
                row["DrivingDistance"] = string.Format("{0:N}", scheduledetail.DrivingDistance);
                row["EstimatedDrivingTime"] = string.Format("{0:N}", scheduledetail.EstimatedDrivingTime * 60);
                row["CustomerName"] = scheduledetail.Name;
                row["Phone"] = scheduledetail.Phone1 + " / " + scheduledetail.Phone2;
                row["CarBrandDescription"] = scheduledetail.CarBrandDescription;
                row["RegistrationNumber"] = scheduledetail.RegistrationNumber;
                row["Model"] = scheduledetail.Model;
                row["Color"] = scheduledetail.Color;
                row["SocketWrenchSize"] = scheduledetail.SocketWrenchSize.ToString();
                if (scheduledetail.HubCaps.HasValue)
                {
                    if (Convert.ToBoolean(scheduledetail.HubCaps))
                    {
                        row["HubCaps"] = "Ja";
                    }
                    else
                    {
                        row["HubCaps"] = "Nej";
                    }
                    
                }
                else
                {
                    row["HubCaps"] = "";
                }
                if (scheduledetail.LockingBolt.HasValue)
                {
                    if (Convert.ToBoolean(scheduledetail.LockingBolt))
                    {
                        row["LockingBolt"] = "Ja";
                    }
                    else
                    {
                        row["LockingBolt"] = "Nej";
                    }

                }
                else
                {
                    row["LockingBolt"] = "";
                }
                if (scheduledetail.RunFlatTire.HasValue)
                {
                    if (Convert.ToBoolean(scheduledetail.RunFlatTire))
                    {
                        row["RunFlatTire"] = "Ja";
                    }
                    else
                    {
                        row["RunFlatTire"] = "Nej";
                    }

                }
                else
                {
                    row["RunFlatTire"] = "";
                }
                if (scheduledetail.RimProtection.HasValue)
                {
                    if (Convert.ToBoolean(scheduledetail.RimProtection))
                    {
                        row["RimProtection"] = "Ja";
                    }
                    else
                    {
                        row["RimProtection"] = "Nej";
                    }

                }
                else
                {
                    row["RimProtection"] = "";
                }
                if (scheduledetail.ExtraBolts.HasValue)
                {
                    if (Convert.ToBoolean(scheduledetail.ExtraBolts))
                    {
                        row["ExtraBolts"] = "Ja";
                    }
                    else
                    {
                        row["ExtraBolts"] = "Nej";
                    }

                }
                else
                {
                    row["ExtraBolts"] = "";
                }
                row["Dimensions"] = scheduledetail.Width + " / " + scheduledetail.Height + " " + scheduledetail.Size.ToString();
                row["Barcode"] = "www.mobilhjulskift.dk/barcodelog.aspx?id=" + scheduledetail.ServiceCarScheduleDetailId.ToString();
                row["Note"] = scheduledetail.Note;
                row["CustomerNote"] = scheduledetail.CustomerNote;
                if (Convert.ToBoolean(scheduledetail.CustomerConfirmed))
                {
                    row["CustomerConfirmed"] = "Ja";
                }
                else
                {
                    row["CustomerConfirmed"] = "Nej";
                }
                row["TaskId"] = scheduledetail.TaskId.ToString();

                dtDataTable.Rows.Add(row);
            }

            #endregion

            return dtDataTable;
        }


        /// <summary>
        /// Return datatable with all taskdetails for a specific task
        /// </summary>
        /// <param name="TaskId">Id on Task</param>
        /// <returns>Datatable with TaskDetail information</returns>
        public DataTable GetTaskDetail(int TaskId)
        {
            // Get all task details

            List<TaskDetailView> taskdetails = this.db.TaskDetailViews.Where(p => p.TaskId == TaskId).OrderBy(p => p.TaskDetailTypeId).ToList();

            // Create datatable
            DataTable dtDataTable = new DataTable();

            #region Create columns in datatable

            DataColumn dcDataColumn;

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.Int32");
            dcDataColumn.ColumnName = "TaskId";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.Int32");
            dcDataColumn.ColumnName = "TaskDetailId";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.Int32");
            dcDataColumn.ColumnName = "TaskDetailTypeId";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Description";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "NoteToEmployee";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "NoteFromEmployee";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "Amount";
            dtDataTable.Columns.Add(dcDataColumn);

            dcDataColumn = new DataColumn();
            dcDataColumn.DataType = Type.GetType("System.String");
            dcDataColumn.ColumnName = "EstimatedTime";
            dtDataTable.Columns.Add(dcDataColumn);

            #endregion

            #region Insert rows into datatable

            foreach (TaskDetailView taskdetail in taskdetails)
            {
                DataRow row;

                row = dtDataTable.NewRow();
                row["TaskId"] = taskdetail.TaskId;
                row["TaskDetailId"] = taskdetail.TaskDetailId;
                row["TaskDetailTypeId"] = taskdetail.TaskDetailTypeId;
                row["Description"] = taskdetail.Description;
                row["NoteToEmployee"] = taskdetail.NoteToEmployee;
                row["NoteFromEmployee"] = taskdetail.NoteFromEmployee;
                row["Amount"] = string.Format("{0:N}", taskdetail.Amount);
                row["EstimatedTime"] = string.Format("{0:N}", taskdetail.EstimatedTime * 60);

                dtDataTable.Rows.Add(row);
            }

            #endregion

            return dtDataTable;
        }
    }
}